//
//  UIGestureRecognizer+ILGDemo.m
//  ILGDemo
//
//  Created by happy on 2020/3/11.
//  Copyright © 2020 Happy. All rights reserved.
//

#import "UIGestureRecognizer+ILGDemo.h"

@implementation UITapGestureRecognizer (ILGDemo)

+ (void)load {
//    NSLog(@"%s", __func__);
    
//    [self ilg_exchangeImplementations:@selector(touchesBegan:withEvent:) swizzleSelector:@selector(ilg_touchesBegan:withEvent:)];
//    [self ilg_exchangeImplementations:@selector(touchesMoved:withEvent:) swizzleSelector:@selector(ilg_touchesMoved:withEvent:)];
//    [self ilg_exchangeImplementations:@selector(touchesEnded:withEvent:) swizzleSelector:@selector(ilg_touchesEnded:withEvent:)];
//    [self ilg_exchangeImplementations:@selector(touchesCancelled:withEvent:) swizzleSelector:@selector(ilg_touchesCancelled:withEvent:)];
}

+ (void)ilg_exchangeImplementations:(SEL)originalSeletor swizzleSelector:(SEL)swizzleSelector
{
    Method originalMethod = class_getInstanceMethod(self, originalSeletor);
    Method swizzledMethod = class_getInstanceMethod(self, swizzleSelector);
    method_exchangeImplementations(originalMethod, swizzledMethod);
}

- (void)ilg_touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s, before super, state = %@", __func__, self.stateDescription);
    [self ilg_touchesBegan:touches withEvent:event];
    NSLog(@"%s, after super, state = %@", __func__, self.stateDescription);
}

- (void)ilg_touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self ilg_touchesMoved:touches withEvent:event];
    NSLog(@"%s, after super, state = %@", __func__, self.stateDescription);
}

- (void)ilg_touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s, before super, state = %@", __func__, self.stateDescription);
    [self ilg_touchesEnded:touches withEvent:event];
    NSLog(@"%s, after super, state = %@", __func__, self.stateDescription);
}

- (void)ilg_touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self ilg_touchesCancelled:touches withEvent:event];
    NSLog(@"%s, after super, state = %@", __func__, self.stateDescription);
}

- (NSString *)stateDescription
{
    NSDictionary *dict = @{@(UIGestureRecognizerStatePossible):@"UIGestureRecognizerStatePossible",
                           @(UIGestureRecognizerStateBegan):@"UIGestureRecognizerStateBegan",
                           @(UIGestureRecognizerStateChanged):@"UIGestureRecognizerStateChanged",
                           @(UIGestureRecognizerStateEnded):@"UIGestureRecognizerStateEnded",
                           @(UIGestureRecognizerStateCancelled):@"UIGestureRecognizerStateCancelled",
                           @(UIGestureRecognizerStateFailed):@"UIGestureRecognizerStateFailed",
                           @(UIGestureRecognizerStateRecognized):@"UIGestureRecognizerStateRecognized"
    };
    
    NSNumber *key = @(self.state);
    return dict[key];
}

@end
